package com.housing.service.impl;

import com.zmetaport.common.core.utils.MapstructUtils;
import com.zmetaport.common.core.utils.StringUtils;
import com.zmetaport.common.mybatis.core.page.TableDataInfo;
import com.zmetaport.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.housing.domain.bo.DictTypeBo;
import com.housing.domain.vo.DictTypeVo;
import com.housing.domain.DictType;
import com.housing.mapper.DictTypeMapper;
import com.housing.service.IDictTypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 字典类型Service业务层处理
 *
 * @author chensm
 * @date 2024-12-15
 */
@RequiredArgsConstructor
@Service
public class DictTypeServiceImpl implements IDictTypeService {

    private final DictTypeMapper baseMapper;

    /**
     * 查询字典类型
     */
    @Override
    public DictTypeVo queryById(Long dictId){
        return baseMapper.selectVoById(dictId);
    }

    /**
     * 查询字典类型列表
     */
    @Override
    public TableDataInfo<DictTypeVo> queryPageList(DictTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DictType> lqw = buildQueryWrapper(bo);
        Page<DictTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询字典类型列表
     */
    @Override
    public List<DictTypeVo> queryList(DictTypeBo bo) {
        LambdaQueryWrapper<DictType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DictType> buildQueryWrapper(DictTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DictType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDictName()), DictType::getDictName, bo.getDictName());
        lqw.eq(StringUtils.isNotBlank(bo.getDictType()), DictType::getDictType, bo.getDictType());
        return lqw;
    }

    /**
     * 新增字典类型
     */
    @Override
    public Boolean insertByBo(DictTypeBo bo) {
        DictType add = MapstructUtils.convert(bo, DictType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDictId(add.getDictId());
        }
        return flag;
    }

    /**
     * 修改字典类型
     */
    @Override
    public Boolean updateByBo(DictTypeBo bo) {
        DictType update = MapstructUtils.convert(bo, DictType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DictType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除字典类型
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
